<?php
namespace Home\Controller;

use \Think\Controller;

class SearchController extends HomeController
{
	public function index()
	{
		$word = I()['word'];

		// 判断是否有关键字输入
		if(empty($word))
		{
			$this->redirect('Index/index');
			exit;
		}

		// 获取要跳转的页数
		$ye = isset($_GET['ye']) ? $_GET['ye'] : 1;
		// 判断获得上一页
		if($ye > 1) {
		 	$newpage = $ye - 1;
		}else {
		  	$newpage = 1;
		}

		$this->assign('nowpage',$ye);

	
		$search = M('book');
		// 统计查询
		$map['name'] = array('like',"%{$word}%");
		$map['author'] = array('like',"%{$word}%");
		$map['_logic'] = 'or';
		$count = $search->where($map)->count();
		// 每页显示的条数
		$page = 3;
		$this->assign('count',$count);
		// 总页数
		if($count == 0) {
			$totalpage = 1;
		}else {
			$totalpage = ceil($count/$page);
		}

		// 判断获得下一页
		if($ye < $totalpage) {
       		$nextpage = $ye + 1;
     	}else {
       		$nextpage = $totalpage;
    	}

    	$this->assign('newpage',$newpage);
    	$this->assign('nextpage',$nextpage);
		$this->assign('totalpage',$totalpage);
		$this->assign('word',$word);
		// 模糊查询
		$data = $search->where($map)->order('id desc')->page("{$ye},{$page}")->select();
		// echo $search->getLastSql();
		$this->assign('list',$data);
		$this->display();
	}

}
